package com.chuhe.starry.controller;


import com.chuhe.common.domain.dto.TokenUserInfoDto;
import com.chuhe.common.domain.enums.UserContactStatusEnum;
import com.chuhe.common.domain.query.PaginationResultVO;
import com.chuhe.common.domain.vo.req.ApplyAddReq;
import com.chuhe.common.domain.vo.req.DealApplyReq;
import com.chuhe.common.domain.vo.resp.*;
import com.chuhe.common.utils.RedisComponet;
import com.chuhe.starry.service.UserContactApplyService;
import com.chuhe.starry.service.UserContactService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 联系人 前端控制器
 * </p>
 *
 * @author 张楚河
 * @since 2025-05-11
 */
@RestController
@RequestMapping("/contact")
@Api(value = "用户联系人相关接口", tags = "用户联系人相关API", description = "用户联系人相关API")
public class UserContactController {
    @Autowired
    private UserContactService userContactService;
    @Autowired
    private RedisComponet redisComponet;
    @Autowired
    private UserContactApplyService userContactApplyService;

    @RequestMapping("/search")
    @ApiOperation("搜索联系人")
    public ApiResult<UserConcactResp> search(HttpServletRequest request, @NotEmpty String contactId){
        TokenUserInfoDto userInfoDto = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(userContactService.searchContact(userInfoDto.getUserId(),contactId));
    }

    @RequestMapping("/applyAdd")
    @ApiOperation("添加联系人")
    public ApiResult<?> applyAdd(HttpServletRequest request, @Valid ApplyAddReq req){
        TokenUserInfoDto userInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(userContactApplyService.applyAdd(userInfo,req));
    }

    @RequestMapping("/loadApply")
    @ApiOperation("加载申请列表")
    public ApiResult<PaginationResultVO<LoadApplyResp>> loadApply(HttpServletRequest request, Integer pageNo){
        TokenUserInfoDto userInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(userContactApplyService.loadApplyByPage(userInfo.getUserId(),pageNo));
    }

    @RequestMapping("/dealWithApply")
    @ApiOperation("处理申请列表")
    public ApiResult<?> dealWithApply(HttpServletRequest request,@Valid DealApplyReq req){
        TokenUserInfoDto userInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(userContactApplyService.dealWithApply(userInfo.getUserId(),req));
    }

    @RequestMapping("/loadContact")
    @ApiOperation("加载联系人")
    public ApiResult<List<loadContactResp>> loadContact(HttpServletRequest request, @NotEmpty String contactType){
        TokenUserInfoDto userInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(userContactService.findContactList(userInfo.getUserId(),contactType));
    }

    @RequestMapping("/getContactInfo")
    @ApiOperation("点击显示联系人详情")
    public ApiResult<loadContactInfoResp> loadContactInfo(HttpServletRequest request, @NotEmpty String contactId){
        TokenUserInfoDto userInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(userContactService.loadContactInfo(userInfo.getUserId(),contactId));
    }

    @RequestMapping("/getContactUserInfo")
    @ApiOperation("加载联系人详情")
    public ApiResult<loadContactInfoResp> getContactUserInfo(HttpServletRequest request, @NotEmpty String contactId){
        TokenUserInfoDto userInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(userContactService.getContactUserInfo(userInfo.getUserId(),contactId));
    }

    @RequestMapping("/delContact")
    @ApiOperation("删除联系人")
    public ApiResult<?> delContact(HttpServletRequest request, @NotEmpty String contactId){
        TokenUserInfoDto userInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(userContactService.delContact(userInfo.getUserId(),contactId, UserContactStatusEnum.DEL));
    }

    @RequestMapping("/addContact2BlackList")
    @ApiOperation("拉黑联系人")
    public ApiResult<?> addContact2BlackList(HttpServletRequest request, @NotEmpty String contactId){
        TokenUserInfoDto userInfo = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(userContactService.delContact(userInfo.getUserId(),contactId, UserContactStatusEnum.BLACKLIST));
    }



}

